#include <stdio.h>
#include "ink_screen.h"
#include "driver/gpio.h"
#include "driver/spi_master.h"

spi_device_handle_t spidev;

void app_main(void)
{
    spi_bus_config_t buscfg = {
        .miso_io_num = -1,
        .mosi_io_num = EPD_MOSI_PIN,
        .sclk_io_num = EPD_SCLK_PIN,
        .quadwp_io_num = -1,
        .quadhd_io_num = -1,
        .max_transfer_sz = 64,
    };
    spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO);
    gpio_set_direction(3, GPIO_MODE_OUTPUT);
    gpio_set_direction(4, GPIO_MODE_OUTPUT);
    gpio_set_direction(5, GPIO_MODE_OUTPUT);
    gpio_set_direction(6, GPIO_MODE_OUTPUT);

    spi_device_interface_config_t devcfg = {
        // .command_bits = 10,
        .clock_speed_hz = 1 * 1000 * 1000,
        .mode = 0, // SPI mode 0
        .spics_io_num = 3,
        .queue_size = 128,
        .input_delay_ns = 10, // the EEPROM output the data half a SPI clock behind.
    };

    spi_bus_add_device(SPI2_HOST, &devcfg, &spidev);

    ink_screen_init(INK_SCREEN_INIT_MODE_NORMAL);
    printf("ink_screen_clear_write\n");
    ink_screen_clear_write();
    vTaskDelay(3000);

    ink_screen_init(INK_SCREEN_INIT_MODE_FAST);
    printf("ink_screen_clear_black\n");
    ink_screen_clear_black();
    vTaskDelay(1000);
    
    ink_screen_sleep();

    while (1)
    {
        vTaskDelay(1000);
    }
}